package cn.edu.scau.cmi.ema.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;

@Entity
@NamedQueries({
		@NamedQuery(name = "findAllProcessmethods", query = "select myProcessmethod from Processmethod myProcessmethod"),
		@NamedQuery(name = "findProcessmethodByName", query = "select myProcessmethod from Processmethod myProcessmethod where myProcessmethod.name = ?1"),
		@NamedQuery(name = "findProcessmethodByNameContaining", query = "select myProcessmethod from Processmethod myProcessmethod where myProcessmethod.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findProcessmethodByFee", query = "select myProcessmethod from Processmethod myProcessmethod where myProcessmethod.fee = ?1"),
		@NamedQuery(name = "findProcessmethodByDescription", query = "select myProcessmethod from Processmethod myProcessmethod where myProcessmethod.description = ?1"),
		@NamedQuery(name = "findProcessmethodByDescriptionContaining", query = "select myProcessmethod from Processmethod myProcessmethod where myProcessmethod.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllProcessmethodsByUnit", query = "select myProcessmethod from Processmethod myProcessmethod where myProcessmethod.unit.id = ?1"),
		@NamedQuery(name = "findProcessmethodById", query = "select myProcessmethod from Processmethod myProcessmethod where myProcessmethod.id = ?1"),
		@NamedQuery(name = "findProcessmethodByPrimaryKey", query = "select myProcessmethod from Processmethod myProcessmethod where myProcessmethod.id = ?1 "),
		@NamedQuery(name = "deleteProcessmethodsByIds", query = "delete Processmethod where id in (?1)"),
		@NamedQuery(name = "findProcessmethodsByIds", query = "select myProcessmethod from Processmethod myProcessmethod where myProcessmethod.id in (?1)"),
		@NamedQuery(name = "findProcessmethodByPhyscialPrimaryKey", query = "select myProcessmethod from Processmethod myProcessmethod where  myProcessmethod.name = ?1 and myProcessmethod.fee = ?2 and myProcessmethod.unit.id = ?3"),
		@NamedQuery(name = "countProcessmethodsByName", query = "select count(myProcessmethod)  from Processmethod myProcessmethod where myProcessmethod.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "processmethod")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Processmethod")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Processmethod implements Serializable, Comparable<Processmethod> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "fee", precision = 22)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	BigDecimal fee;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "unit", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Unit unit;
	@JSONField(serialize = false)
	@ManyToMany(mappedBy = "processmethodsForFood", fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Food> foodsForProcessmethod;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setFee(BigDecimal fee) {
		this.fee = fee;
	}

	public BigDecimal getFee() {
		return this.fee;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	public Unit getUnit() {
		return unit;
	}

	public void setFoodsForProcessmethod(Set<Food> foodsForProcessmethod) {
		this.foodsForProcessmethod = foodsForProcessmethod;
	}

	public Set<Food> getFoodsForProcessmethod() {
		if (foodsForProcessmethod == null) {
			foodsForProcessmethod = new java.util.LinkedHashSet<Food>();
		}
		return foodsForProcessmethod;
	}

	public void copy(Processmethod that) {

		setId(that.getId());

		setName(that.getName());
		setFee(that.getFee());
		setDescription(that.getDescription());

		setUnit(that.getUnit());

		setFoodsForProcessmethod(
				new java.util.LinkedHashSet<Food>(that.getFoodsForProcessmethod()));

	}

	public Processmethod() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("fee=[").append(fee).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Processmethod))
			return false;
		Processmethod comparedProcessmethod = (Processmethod) obj;
		if (!CheckPropertyEqual(this.name, comparedProcessmethod.name)) {
			return false;
		}
		if (!CheckPropertyEqual(this.fee, comparedProcessmethod.fee)) {
			return false;
		}
		if (!CheckPropertyEqual(this.unit.id, comparedProcessmethod.unit.id)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Processmethod o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
